package com.toune.movie.mvp.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.siberiadante.customdialoglib.BottomPopupWindow;
import com.siberiadante.customdialoglib.EditDialog;
import com.toune.movie.R;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.BasePresenterImpl;
import com.vondear.rxtool.RxFileTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.RxRunTextView;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyDataActivity extends BaseActivity {

    private static final int SELECTPHOTO = 101;
    private static final int TAKEPHOTO = 102;
    @BindView(R.id.headIv)
    ImageView headIv;
    @BindView(R.id.headRl)
    RelativeLayout headRl;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.nickRl)
    RelativeLayout nickRl;
    @BindView(R.id.signTv)
    RxRunTextView signTv;
    @BindView(R.id.signRl)
    RelativeLayout signRl;
    @BindView(R.id.nameRl)
    RelativeLayout nameRl;
    @BindView(R.id.nickTv)
    TextView nickTv;

    @Override
    public void init(Bundle savedInstanceState) {
        updateView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_my_data;
    }

    @Override
    protected String getTitleStr() {
        return "我的资料";
    }

    @Override
    protected void initEventAndData() {

    }

    private void updateView() {
        if (AVUser.getCurrentUser() != null) {
            if (AVUser.getCurrentUser().getAVFile("image") != null) {
                String imageUrl = AVUser.getCurrentUser().getAVFile("image").getUrl();
                Glide.with(context).load(imageUrl)
                        .apply(new RequestOptions().circleCrop()).into(headIv);
            }
            nameTv.setText(AVUser.getCurrentUser().getUsername());
            nickTv.setText(AVUser.getCurrentUser().getString("nick"));
            signTv.setText(AVUser.getCurrentUser().getString("sign"));
        }
    }

    private void postName(final String msg) {
        AVUser currentUser = AVUser.getCurrentUser();
        currentUser.put("nick", msg);
        currentUser.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    nickTv.setText(msg);
                }
            }
        });
    }


    private void takePhoto() {
        String filePath = RxFileTool.getSDCardPath() + new Date().getTime();
        Album.camera(this) // 相机功能。
                .image() // 拍照。
                .filePath(filePath) // 文件保存路径，非必须。
                .requestCode(TAKEPHOTO)
                .onResult(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {
                        postPhoto1(result);
                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {
                        RxToast.info(result);
                    }
                })
                .start();
    }

    private void postPhoto1(final String reuslt) {

        try {
            final AVFile avFile = AVFile.withFile(RxFileTool.getFileName(reuslt), RxFileTool.getFileByPath(reuslt));
            avFile.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        Glide.with(context).load(avFile.getUrl()).
                                apply(new RequestOptions().circleCrop()).into(headIv);
                        AVUser currentUser = AVUser.getCurrentUser();
                        currentUser.put("image", avFile);
                        currentUser.saveInBackground();
                    }
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    private void seclectPhote() {
        Album.image(this) // 选择图片。
                .multipleChoice()
                .requestCode(SELECTPHOTO)
                .camera(false)
                .columnCount(4)
                .selectCount(1)
                .checkedList(null)
                .filterSize(null)
                .filterMimeType(null)
                .afterFilterVisibility(false) // 显示被过滤掉的文件，但它们是不可用的。
                .onResult(new Action<ArrayList<AlbumFile>>() {
                    @Override
                    public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                        postPhoto1(result.get(0).getPath());
                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {
                        RxToast.info(result);
                    }
                })
                .start();
    }

    @Override
    public BasePresenterImpl initPresenter() {
        return null;
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @OnClick({R.id.headRl, R.id.nickRl, R.id.signRl, R.id.nameRl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nameRl:
                RxToast.info("暂不提供修改用户名");
                break;
            case R.id.headRl:
                BottomPopupWindow bottomPopupWindow = new BottomPopupWindow(context).builder();
                bottomPopupWindow.setTitle("设置头像");
                bottomPopupWindow.addSheetItem("拍一张照片", BottomPopupWindow.SheetItemColor.Blue, new BottomPopupWindow.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int i) {
                        takePhoto();
                    }
                }).addSheetItem("从相册选一张", BottomPopupWindow.SheetItemColor.Blue, new BottomPopupWindow.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int i) {
                        seclectPhote();
                    }
                }).setCanceled(true)
                        .show();
                break;
            case R.id.nickRl:
                final EditDialog editDialog = new EditDialog(context).builder();
                editDialog.setTitle("昵称").setPositiveButton("提交", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        postName(editDialog.getMsg());
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        editDialog.dismiss();
                    }
                }).show();
                break;
            case R.id.signRl:
                final EditDialog editDialog1 = new EditDialog(context).builder();
                editDialog1.setTitle("个性签名").setPositiveButton("提交", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        postSign(editDialog1.getMsg());
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        editDialog1.dismiss();
                    }
                }).show();
                break;
        }
    }

    private void postSign(final String msg) {
        AVUser.getCurrentUser().put("sign", msg);
        AVUser.getCurrentUser().saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    signTv.setText(msg);
                }
            }
        });
    }
}
